package com.utile;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;

public class MyToken {

    static final Algorithm ALG = Algorithm.HMAC256("123123");

    public String getToken(String username,int id,int version){
//        时间
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.SECOND,10000);
//        生成令牌
        String token= JWT.create()
//                令牌中包含的信息
                .withClaim("id",id)
                .withClaim("name",username)
                .withClaim("version",version)
//                令牌存活的时间
                .withExpiresAt(calendar.getTime())
//                设置签名
                .sign(ALG);
        return token;
    }
    public String[] getNew(String token){
        JWTVerifier jwtVerifier=JWT.require(ALG).build();
//        解析令牌
        DecodedJWT verify = jwtVerifier.verify(token);
//        获取令牌的信息值
        Claim name = verify.getClaim("name");
        String s = name.asString();
        Claim id1 = verify.getClaim("id");
        String s1 = id1.asString();
        Claim version2 = verify.getClaim("version");
        String s2 = version2.asString();
        String[] arr=new String[]{s,s1,s2};
        return  arr;
    }
}
